<?php

/**
 * 跳转
 * @param string $msg 跳转提示
 * @param string $gourl 动作类型
 */
if (!function_exists('show_msg')) {
    function show_msg($msg = '', $gourl = '-1')
    {
        if ($gourl == '-1')
            echo '<script>alert("' . $msg . '");history.go(-1);</script>';
    
        else if ($gourl == '0')
            echo '<script>alert("' . $msg . '");location.reload();</script>';
    
        else
            echo '<script>alert("' . $msg . '");location.href="' . $gourl . '";</script>';
    }
}

/**
 * 跨域返回json
 * @param array $data 返回数组
 */
 
if (!function_exists('ajax_json_return')) {
    function ajax_json_return($data)
    {
        header('Content-Type:application/json; charset=utf-8');
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
        echo json_encode($data);
        exit();
    }
}
/**
 * 获取上传文件类型
 * @param string $type 上传类型
 */
if (!function_exists('get_upload_type')) {
    function get_upload_type($type = '')
    {
        $cfg_upload_img_type = C('cfg_upload_img_type');
        $cfg_upload_soft_type = C('cfg_upload_soft_type');
        $cfg_upload_media_type = C('cfg_upload_media_type');
    
        if ($type == 'image') {
            $uptype = explode('|', $cfg_upload_img_type);
            $upstr = '';
            foreach ($uptype as $v) {
                if (!empty($v)) {
                    $upstr .= '*.' . $v . ';';
                }
            }
            return $upstr;
        } else if ($type == 'soft') {
            $uptype = explode('|', $cfg_upload_soft_type);
            $upstr = '';
            foreach ($uptype as $v) {
                if (!empty($v)) {
                    $upstr .= '*.' . $v . ';';
                }
            }
            return $upstr;
        } else if ($type == 'media') {
            $uptype = explode('|', $cfg_upload_media_type);
            $upstr = '';
            foreach ($uptype as $v) {
                if (!empty($v)) {
                    $upstr .= '*.' . $v . ';';
                }
            }
            return $upstr;
        } else if ($type == 'all') {
            $alltype = $cfg_upload_img_type . '|' . $cfg_upload_soft_type . '|' . $cfg_upload_media_type;
            $uptype = explode('|', $alltype);
            $upstr = '';
            foreach ($uptype as $v) {
                if (!empty($v)) {
                    $upstr .= '*.' . $v . ';';
                }
            }
            return $upstr;
        } else {
            return $type;
        }
    }
}
/**
 * 获取上传文件描述
 * @param string $type 上传类型
 */
if (!function_exists('get_upload_desc')) {
    function get_upload_desc($desc)
    {
        if ($desc == 'image') return '图像类型:';
    
        else if ($desc == 'soft') return '软件类型:';
    
        else if ($desc == 'media') return '媒体类型:';
    
        else if ($desc == 'all') return '所有类型:';
    
        else return $desc;
    }
}
/**
 * 获取上传文件描述
 * @param string $type 上传类型
 */
if (!function_exists('get_real_size')) {
    function get_real_size($size)
    {
        $kb = 1024;          // Kilobyte
        $mb = 1024 * $kb;    // Megabyte
        $gb = 1024 * $mb;    // Gigabyte
        $tb = 1024 * $gb;    // Terabyte
    
        if ($size < $kb)
            return $size . 'B';
    
        else if ($size < $mb)
            return round($size / $kb, 2) . 'KB';
    
        else if ($size < $gb)
            return round($size / $mb, 2) . 'MB';
    
        else if ($size < $tb)
            return round($size / $gb, 2) . 'GB';
    
        else
            return round($size / $tb, 2) . 'TB';
    }
}
 
/**
 * 返回输入数组中某个单一列的值
 * @param int $show_time 时间戳
 */
if (!function_exists('array_column')) {
    function array_column($input, $columnKey, $indexKey = null)
    {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
        $indexKeyIsNull = (is_null($indexKey)) ? true : false;
        $indexKeyIsNumber = (is_numeric($indexKey)) ? true : false;
        $result = array();
        foreach ((array)$input as $key => $row) {
            if ($columnKeyIsNumber) {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
            } else {
                $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
            }
            if (!$indexKeyIsNull) {
                if ($indexKeyIsNumber) {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key)) ? current($key) : null;
                    $key = is_null($key) ? 0 : $key;
                } else {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    }
}

/**
 * 处理时间戳
 * @param int $show_time 时间戳
 */
if (!function_exists('format_time')) {
    function format_time($show_time)
    {
        $now_time = time();
        $dur = $now_time - $show_time;
        $the_time = date('Y-m-d H:i:s', $show_time);
        if ($dur < 0) {
            return $the_time;
        } else {
            if ($dur < 60) {
                return $dur . '秒前';
            } else {
                if ($dur < 3600) {
                    return floor($dur / 60) . '分钟前';
                } else {
                    if ($dur < 86400) {
                        return floor($dur / 3600) . '小时前';
                    } else {
                        if ($dur < 259200) {//3天内
                            return floor($dur / 86400) . '天前';
                        } else {
                            return $the_time;
                        }
                    }
                }
            }
        }
    }
}

/**
 * 获取字符串首字母大写拼音
 * @param string $s0 字符串
 */
if (!function_exists('first_pinyin')) {
    function first_pinyin($s0)
    {
        $fchar = ord($s0{0});
        if ($fchar >= ord("A") and $fchar <= ord("z")) return strtoupper($s0{0});
        $s1 = iconv("UTF-8", "GBK//IGNORE", $s0);
        $s2 = iconv("GBK", "UTF-8//IGNORE", $s1);
        if ($s2 == $s0) {
            $s = $s1;
        } else {
            $s = $s0;
        }
        @$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if ($asc >= -20319 and $asc <= -20284) return "A";
        if ($asc >= -20283 and $asc <= -19776) return "B";
        if ($asc >= -19775 and $asc <= -19219) return "C";
        if ($asc >= -19218 and $asc <= -18711) return "D";
        if ($asc >= -18710 and $asc <= -18527) return "E";
        if ($asc >= -18526 and $asc <= -18240) return "F";
        if ($asc >= -18239 and $asc <= -17923) return "G";
        if ($asc >= -17922 and $asc <= -17418) return "H";
        if ($asc >= -17417 and $asc <= -16475) return "J";
        if ($asc >= -16474 and $asc <= -16213) return "K";
        if ($asc >= -16212 and $asc <= -15641) return "L";
        if ($asc >= -15640 and $asc <= -15166) return "M";
        if ($asc >= -15165 and $asc <= -14923) return "N";
        if ($asc >= -14922 and $asc <= -14915) return "O";
        if ($asc >= -14914 and $asc <= -14631) return "P";
        if ($asc >= -14630 and $asc <= -14150) return "Q";
        if ($asc >= -14149 and $asc <= -14091) return "R";
        if ($asc >= -14090 and $asc <= -13319) return "S";
        if ($asc >= -13318 and $asc <= -12839) return "T";
        if ($asc >= -12838 and $asc <= -12557) return "W";
        if ($asc >= -12556 and $asc <= -11848) return "X";
        if ($asc >= -11847 and $asc <= -11056) return "Y";
        if ($asc >= -11055 and $asc <= -10247) return "Z";
        return "#";
    }
}

/**
 * 获取上传文件描述
 * @param string $url 接口路径
 * @param array $post_data post请求数组
 */
if (!function_exists('post_curl')) {
    function post_curl($url, $post_data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, TRUE);
    }
}

/**
 * 验证手机号是否正确
 * @param int $mobile 手机号
 */
if (!function_exists('is_mobile')) {
    function is_mobile($mobile)
    {
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }
}

/**
 * 验证短信验证码是否正确
 * @param int $code 验证码
 */
if (!function_exists('is_code')) {
    function is_code($code)
    {
        return preg_match('/^\d{4}$/', $code) ? true : false;
    }
}

/**
 * 验证短信验证码是否正确
 * @param string $password 密码
 */
if (!function_exists('is_password')) {
    function is_password($password)
    {
        return preg_match('/^[a-zA-Z0-9]{5,20}$/', $password) ? true : false;
    }
}

